#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_pwm.h"

#define LED_GPIO_IDX 6

#define LED_USE_PWM 1
#define LED_PWM_CHN 3

int g_light_state;

int light_init(){

    IoTGpioInit(LED_GPIO_IDX);
    IoTGpioSetDir(LED_GPIO_IDX,IOT_GPIO_DIR_OUT);
#if LED_USE_PWM==1
    IoTGpioSetFunc(LED_GPIO_IDX, IOT_GPIO_FUNC_GPIO_6_PWM3_OUT);
    IoTPwmInit(LED_PWM_CHN);
#else
    IoTGpioSetFunc(LED_GPIO_IDX, IOT_GPIO_FUNC_GPIO_6_GPIO);
#endif
}
int light_onoff(int onoff){
    
#if LED_USE_PWM==1
    if(onoff == 0){
        IoTPwmStop(LED_PWM_CHN);
    }else{
        IoTPwmStart(LED_PWM_CHN, 99, 10000);
    }
   
#else    
    IoTGpioSetOutputVal(LED_GPIO_IDX,onoff ? 1 : 0);
#endif

    g_light_state = onoff ? 1 : 0;

}

int light_brightness(int percent)
{
    if(percent >= 100){
        percent = 99;
    }
    if(percent <= 0){
        percent == 0;
        IoTPwmStop(LED_PWM_CHN);
        return 0;
    }
    IoTPwmStart(LED_PWM_CHN, percent, 10000);

    return 0;
}


